
import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HttpClientRoute extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HttpClientRouteState();
  }

}

Future<String> _http() async {
  HttpClient client = HttpClient();
  Uri uri = Uri.parse("https://api.apiopen.top/getWangYiNews");
  var request = await client.postUrl(uri);
  var response = await request.close();
  if (response.statusCode == 200) {
    return response.transform(utf8.decoder).join();
  } else {
    print(response.statusCode);
    return '请求失败';
  }
}

Widget _widget() {
  return FutureBuilder(
    future: _http(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.hasError) {
          return Text(snapshot.error.toString());
        }
        return Scrollbar(
          child: SingleChildScrollView(
            child: Text(snapshot.data.toString()),
          )
        );
      } else {
        return CircularProgressIndicator();
      }
    },
  );
}

class HttpClientRouteState extends State<HttpClientRoute> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('HttpClient'),
      ),
      body: Center(
        child: _widget(),
      ),
    );
  }

}